/**
 * 作者:hanbo
 * 日期:2018/11/1917:44
 * 描述:线绘制
 */
import {VectorBrush} from "./VectorBrush";


export class PolygonsBrush extends VectorBrush {
    constructor(webGL) {
        super(webGL)
    }


    /**
     * 批量绘制线
     * @param float32Array {Float32Array} 缓冲区数组
     * @param index {Number[]} 线数量
     * @param style {PolygonStyle} 多边形样式
     */
    drawPolygons(float32Array,index,style) {
        this._verticesColors=float32Array;
        this._index=index;
        this._style=style;

    }

    draw(projection) {
        super.draw(projection);
        this._compile();
    }

    _compile() {
        this.createBuffer(this._verticesColors);
        let a_Position = this._webGL.getAttribLocation(this._program, 'a_Position');
        if (a_Position < 0) {
            console.error('获取a_Position存储位置失败');
            return -1;
        }
        let a_Color = this._webGL.getAttribLocation(this._program, 'a_Color');
        if (a_Color < 0) {
            console.error('获取a_Color存储失败');
            return -1;
        }
        const FSIZE = Float32Array.BYTES_PER_ELEMENT;
        if(this._style){
            this._webGL.vertexAttribPointer(a_Position, 3, this._webGL.FLOAT, false, FSIZE * 3, 0);
            this._webGL.enableVertexAttribArray(a_Position);
            //画里面的面
            if(this._style.fillStyle){
                let fillStyle=this._style.fillStyle;
                this._webGL.vertexAttrib4f(a_Color,fillStyle.color.r,fillStyle.color.g,fillStyle.color.b,fillStyle.opacity);
                let start_position=0;
                for (let i = 0; i <this._index.length ; i++) {
                    this._webGL.drawArrays(this._webGL.TRIANGLE_FAN, start_position, this._index[i]);
                    start_position+=this._index[i];
                    console.log("绘制",i)
                }
            }
            //画外面的0宽度线
            if(this._style.lineStyle){
                let lineStyle=this._style.lineStyle;
                this._webGL.vertexAttrib4f(a_Color,lineStyle.color.r,lineStyle.color.g,lineStyle.color.b,lineStyle.opacity);
                let start_position=0;
                for (let i = 0; i <this._index.length ; i++) {
                    this._webGL.drawArrays(this._webGL.LINE_LOOP, start_position, this._index[i]);
                    start_position+=this._index[i];
                }
            }

        }else {
            this._webGL.vertexAttribPointer(a_Position, 2, this._webGL.FLOAT, false, FSIZE * 6, 0);
            this._webGL.enableVertexAttribArray(a_Position);
            this._webGL.vertexAttribPointer(a_Color, 4, this._webGL.FLOAT, false, FSIZE * 6, FSIZE * 2);
            this._webGL.enableVertexAttribArray(a_Color);
        }
    }
}